/**
 * 同步第三方数据
 * @author yanwl 
 * @date 2015-02-12
 */
package com.chnbst.mbstyun.controller;

import java.util.Date;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Component;

import com.chnbst.mbstyun.utils.HttpRequest;
import com.chnbst.mbstyun.utils.ReadProperties;
import com.chnbst.mbstyun.utils.SignUtil;


@Component
public class Eos {
	
	/*获取虚拟号*/
	@SuppressWarnings("unchecked")
	public String getVn() throws NumberFormatException, Exception {
		String getVnUrl = ReadProperties.loadProperties().getProperty("GETVNURL");
		System.out.println("getVnUrl:"+getVnUrl);
		
		long startTime = System.currentTimeMillis();
		System.out.println("获取虚拟号开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(getVnUrl, "");
		long endTime = System.currentTimeMillis();
		System.out.println("获取虚拟号结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		System.out.println("获取虚拟号调用接口时间毫秒数："+(endTime-startTime));
		System.out.println(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
	
	/*回收虚拟号*/
	@SuppressWarnings("unchecked")
	public String recycleVn(String vn) throws NumberFormatException, Exception {
		String recycleVnUrl = ReadProperties.loadProperties().getProperty("RECYCLEVNURL");
		String params = "number="+vn;
		System.out.println("recycleVnUrl:"+recycleVnUrl);
		System.out.println("params:"+params);
		
		long startTime = System.currentTimeMillis();
		System.out.println("回收虚拟号开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(recycleVnUrl, params);
		long endTime = System.currentTimeMillis();
		System.out.println("回收虚拟号结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		System.out.println("回收虚拟号调用接口时间毫秒数："+(endTime-startTime));
		System.out.println(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
	
	/*新增IP电话*/
	@SuppressWarnings("unchecked")
	public String addIp(String vn,String password) throws NumberFormatException, Exception {
		String addIpUrl = ReadProperties.loadProperties().getProperty("ADDIPURL");
		String params = "vnumber="+vn+"&password="+password;
		System.out.println("addIpUrl:"+addIpUrl);
		System.out.println("params:"+params);
		
		long startTime = System.currentTimeMillis();
		System.out.println("新增IP电话开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(addIpUrl, params);
		long endTime = System.currentTimeMillis();
		System.out.println("新增IP电话结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		System.out.println("新增IP电话调用接口时间毫秒数："+(endTime-startTime));
		System.out.println(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
	
	/*修改IP电话密码*/
	@SuppressWarnings("unchecked")
	public String updateIpPwd(String vn,String oldPwd,String newPwd) throws NumberFormatException, Exception {
		String updateIpPwdUrl = ReadProperties.loadProperties().getProperty("UPDATEIPPWDURL");
		String params = "vnumber="+vn+"&oldpassword="+oldPwd+"&newpassword="+newPwd;
		System.out.println("updateIpPwdUrl:"+updateIpPwdUrl);
		
		long startTime = System.currentTimeMillis();
		System.out.println("修改IP电话密码开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(updateIpPwdUrl, params);
		long endTime = System.currentTimeMillis();
		System.out.println("修改IP电话密码结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		System.out.println("修改IP电话密码调用接口时间毫秒数："+(endTime-startTime));
		System.out.println(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
	
	/*删除IP电话*/
	@SuppressWarnings("unchecked")
	public String deleteIp(String vn) throws NumberFormatException, Exception {
		String deleteUrl = ReadProperties.loadProperties().getProperty("DELETEIPURL");
		String params = "vnumber="+vn;
		System.out.println("deleteUrl:"+deleteUrl);
		System.out.println("params:"+params);
		
		long startTime = System.currentTimeMillis();
		System.out.println("删除IP电话开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(deleteUrl, params);
		long endTime = System.currentTimeMillis();
		System.out.println("删除IP电话结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		System.out.println("删除IP电话调用接口时间毫秒数："+(endTime-startTime));
		System.out.println(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
}
